//
//  RJCheckDataTopTableVC.m
//  AirQuality
//
//  Created by Po on 15/3/16.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCheckDataTopTableVC.h"
#import "RJCheckDataFitCell.h"

@interface RJCheckDataTopTableVC ()
@property (retain, nonatomic) NSArray * data;       //数据@
@property (retain, nonatomic) NSArray * keys;       //删选类型数组

- (void)initInterface;
@end

@implementation RJCheckDataTopTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initInterface];
}

- (void)initInterface
{
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setScrollEnabled:NO];
}

#pragma mark - function
- (void)setData:(NSArray *)data time:(NSUInteger)time keys:(NSArray *)keys
{
    _keys = [NSArray arrayWithArray:keys];
    _data = [self getMaxAndMinWithData:data time:(NSUInteger)time];
    [self.tableView reloadData];
}

- (void)setCellKeys:(NSArray *)keys
{
    _keys = [NSArray arrayWithArray:keys];
    [self.tableView reloadData];
}

/**整理数据，获得最大，最小，平均*/
- (NSArray *)getMaxAndMinWithData:(NSArray *)data time:(NSUInteger)time
{
    NSMutableDictionary * maxArray = [NSMutableDictionary dictionary];
    NSMutableDictionary * minArray = [NSMutableDictionary  dictionary];
    NSMutableDictionary * averArray = [NSMutableDictionary  dictionary];
    
    //检测
    for (NSString * key in _keys) {
        NSNumber * max = [data[0] objectForKey:key];
        NSNumber * min = [data[0] objectForKey:key];
        NSNumber * aver = @0;
        //计算
        NSUInteger count = 24;
        for (NSUInteger i = 0; i <  time; i ++) {
            NSDictionary * dic = data[i];
            NSNumber * num = [dic objectForKey:key];
            if ([num floatValue] == 0) {
                count --;
                continue;
            }
            //累计平均值
            if ([key isEqualToString:@"co"]) {
                aver = @([aver floatValue] + [num floatValue]);
            } else {
                aver = @([aver integerValue] + [num integerValue]);
            }
            //判断最大
            if ([max compare:num] == NSOrderedAscending) {
                max = num;
            } else if ([min compare:num] == NSOrderedDescending) {
                min = num;
            }
        }
        //是否为co
        if ([key isEqualToString:@"co"]) {
            if (count == 0) {
                aver = @0;
            } else {
                aver = [NSNumber numberWithDouble:[[NSString stringWithFormat:@"%.2f",([aver floatValue] / count)] doubleValue]];
            }
            max = [NSNumber numberWithDouble:[[NSString stringWithFormat:@"%.2f",[max floatValue]] doubleValue]];
            min = [NSNumber numberWithDouble:[[NSString stringWithFormat:@"%.2f",[min floatValue]] doubleValue]];
        } else {
            if (count == 0) {
                aver = @0;
            } else {
                aver = @([aver integerValue] / count);
            }
        }
        [maxArray setObject:max forKey:key];
        [minArray setObject:min forKey:key];
        [averArray setObject:aver forKey:key];
    }
    
    return [NSArray arrayWithObjects:maxArray, minArray, averArray,nil];
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self getSectionViewWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
}

- (UIView *)getSectionViewWithFrame:(CGRect)frame
{
    NSUInteger index = _keys.count + 1;
    CGFloat width = frame.size.width / index;
    CGFloat height = frame.size.height;
    UIView * view = [[UIView alloc] initWithFrame:frame];
    [view setBackgroundColor:GetRGBColor(245, 245, 245, 1)];
    NSArray * titles = [RJTools getTrueCellTitleWithKeys:_keys];
    for (NSUInteger i = 0; i < index; i ++) {
        for (NSUInteger j = 0; j < 2; j ++) {
            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(width * i,
                                                                           (height / 5 * 3) * j,
                                                                           width,
                                                                           height / 5 * ((j == 0) ? 4 : 1))];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //字体
            CGFloat font;
            if (j == 0) {
                font = 13;
            } else {
                font = 10;
            }
            [button.titleLabel setFont:[UIFont systemFontOfSize:font]];
            //标题
            if (i == 0) {
                [button setTitle:(j == 0) ? @"类型":@"" forState:UIControlStateNormal];
            } else {
                NSString * string;
                if (j == 0) {
                    string = titles[i - 1];
                } else {
                    string = (i == (index - 1)) ? @"(mg)" :@"(ug)";
                }
                [button setTitle:string forState:UIControlStateNormal];
            }
            [view addSubview:button];
        }
    }
    
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 25.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellIdent = @"RJCheckDataCell";
    RJCheckDataFitCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        cell = [[RJCheckDataFitCell alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 25) buttonNum:_keys.count + 1];
        [cell setAllEfficacy:NO];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
        [cell setAllFont:[UIFont systemFontOfSize:14]];
    }
    if (_data.count != 0) {
        //获取数据
        NSDictionary * temp = _data[indexPath.row];
        for (NSUInteger i = 0; i < _keys.count + 1; i ++) {
            RJCheckDataCellButton * button = cell.allButtons[i];
            if (i == 0) {
                [button setTitle:(indexPath.row == 0 ? @"最大值" :(indexPath.row) == 1 ? @"最小值" : @"均值") forState:UIControlStateNormal];
                if (DeviceType < 6) {
                    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
                }
            } else {
                NSString * key = _keys[i - 1];
                NSString * string = GetStringWithNum(temp[key]);
                if ([key isEqualToString:@"co"]) {
                    string = [NSString stringWithFormat:@"%.2f",[string floatValue]];
                }
                [button setTitle:string  forState:UIControlStateNormal];
            }
        }
    }

    return cell;
}




@end
